﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;
using System.Diagnostics;

namespace TestCassiniHost
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Process cassini;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var cassiniPath = string.Empty;
            var solutionPath = Path.Combine(Environment.ExpandEnvironmentVariables("%SFSOLUTIONPATH%"), "SolutionFramework.Web");

            // Kill all current instances         

            var name = Path.GetFileNameWithoutExtension("WebDev.WebServer40.EXE");

            foreach (Process proc in Process.GetProcessesByName(name))
            {
                proc.Kill();
            }

            if (Environment.Is64BitOperatingSystem)
            {
                cassiniPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE");
            }
            else
            {
                cassiniPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Common Files\microsoft shared\DevServer\10.0\WebDev.WebServer40.EXE");
            }

            var startInfo = new ProcessStartInfo
            {
                FileName = cassiniPath,
                Arguments = string.Format("/port:{0} /path:\"{1}\"", 5998, solutionPath)
            };

            cassini = Process.Start(startInfo);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (cassini != null)
            {
                cassini.Kill();
            }
        }

        private void cmdBrowse_Click(object sender, RoutedEventArgs e)
        {
            Browser.Navigate("http://localhost:5998/Default.aspx");
        }
    }
}
